﻿// ██
// ██

/**
 * 方块1的构造方法。
 * @return 方块1
 */
function Block1() {
    // 创建方块
    const block = new Block()

    // 初始化数据
    block.mBlocks[1][1].mUsed = block.mBlocks[1][2].mUsed = block.mBlocks[2][1].mUsed = block.mBlocks[2][2].mUsed = true

    // 初始化函数
    block.enterPreview = Block1_EnterPreview
    block.erase = Block1_Erase
    block.getBottom = Block1_GetBottom
    block.getTop = Block1_GetTop
    block.handleCannotMoveDown = Block1_HandleCannotMoveDown
    block.moveDown = Block1_MoveDown
    block.moveLeft = Block1_MoveLeft
    block.moveRight = Block1_MoveRight
    block.transform = Block1_Transform

    return block
}

/**
 * 进入预览区域。
 * @param block     方块1
 * @param drawer    绘图者
 */
function Block1_EnterPreview(block, drawer) {
    const x = Const.PREVIEW_WIDTH / 2 - Const.GRID_SIZE
    const y = Const.PREVIEW_HEIGHT / 2 - Const.GRID_SIZE

    for (var i = 0; i < 2; i++) {
        for (var j = 0; j < 2; j++) {
            drawer.fillStyle = block.mColor
            drawer.fillRect(x + Const.GRID_SIZE * j, y + Const.GRID_SIZE * i, Const.GRID_SIZE, Const.GRID_SIZE)
            Block.drawEdge(drawer, x + Const.GRID_SIZE * j, y + Const.GRID_SIZE * i)
        }
    }
}

/**
 * 擦除。
 * @param block 方块1
 */
function Block1_Erase(block) {
    block.eraseRect(block, Const.GRID_SIZE * (block.mLine + 1), Const.GRID_SIZE * (block.mRow + 1), Const.GRID_SIZE * 2, Const.GRID_SIZE * 2)
}

/**
 * 获取底端所在的行数。底端是指方块最下方的有效格。
 * @param block 方块1
 * @return 底端所在的行数
 */
function Block1_GetBottom(block) {
    return block.mRow + 2
}

/**
 * 获取顶端所在的行数。顶端是指方块最上方的有效格。
 * @param block 方块1
 * @return 顶端所在的行数
 */
function Block1_GetTop(block) {
    return block.mRow + 1
}

/**
 * 对不能下落的处理。
 * @param block 方块1
 */
function Block1_HandleCannotMoveDown(block) {
    gExistentBlocks[block.mRow + 1][block.mLine + 1].mUsed =
    gExistentBlocks[block.mRow + 1][block.mLine + 2].mUsed =
    gExistentBlocks[block.mRow + 2][block.mLine + 1].mUsed =
    gExistentBlocks[block.mRow + 2][block.mLine + 2].mUsed = true
    gExistentBlocks[block.mRow + 1][block.mLine + 1].m_color =
    gExistentBlocks[block.mRow + 1][block.mLine + 2].m_color =
    gExistentBlocks[block.mRow + 2][block.mLine + 1].m_color =
    gExistentBlocks[block.mRow + 2][block.mLine + 2].m_color = block.m_color
}

/**
 * 下落。
 * @param block 方块1
 * @return 成功则返回 true，否则返回 false
 */
function Block1_MoveDown(block) {
    if (block.mRow == 17) {
        block.handleCannotMoveDown(block)
        return false 
    }
    if (gExistentBlocks[block.mRow + 3][block.mLine + 1].mUsed ||
        gExistentBlocks[block.mRow + 3][block.mLine + 2].mUsed) {
        block.handleCannotMoveDown(block)
        return false
    }

    return Block_MoveDown(block)
}

/**
 * 左移。
 * @param block 方块1
 */
function Block1_MoveLeft(block) {
    if (block.mLine == -1) {
        return
    }
    if (gExistentBlocks[block.mRow + 1][block.mLine].mUsed ||
        gExistentBlocks[block.mRow + 2][block.mLine].mUsed) {
        return
    }

    Block_MoveLeft(block)
}

/**
 * 右移。
 * @param block 方块1
 */
function Block1_MoveRight(block) {
    if (block.mLine == 7) {
        return
    }
    if (gExistentBlocks[block.mRow + 1][block.mLine + 3].mUsed ||
        gExistentBlocks[block.mRow + 2][block.mLine + 3].mUsed) {
        return
    }

    Block_MoveRight(block)
}

/**
 * 变形。
 * @param block 方块1
 */
function Block1_Transform(block) {
}
